<template>
    <PopList :title="'计算差异'" :showButton="true" @close="ok" @ok="scene" :leftButton="'计算'" :rightButton="'确认'"
        :showLeftButton="true">
        <LabelInput v-model="onePolyId" :label="'多边形1'" :checkbox="true" :list="onePolyList"
            :checkboxFun="changeOnePolygonShow" :liClickFun="changeOnePolygonId" :placeholder="'请选择或输入'"></LabelInput>
        <LabelInput v-model="twoPolyId" :label="'多边形2'" :checkbox="true" :list="twoPolyList"
            :checkboxFun="changeTwoPolygonShow" :liClickFun="changeTwoPolygonId" :placeholder="'请选择或输入'"></LabelInput>
    </PopList>
</template>
<script setup lang="ts">
import { Message } from "earthsdk-ui"
import { ESGeoPolygon, ESPolygonWithHole } from "esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main";
import { ref, inject, onBeforeUnmount } from "vue";
import PopList from "../../../components/PopList.vue";
import { createSceneObjTreeItemFromJson } from "../../plotting/esObj/fun";
import { searchForPolygonValues } from "./fun";
import { XbsjEarthUi } from "../../../scripts/xbsjEarthUi";
const xbsjEarthUi = inject('xbsjEarthUi') as XbsjEarthUi
const emits = defineEmits(['close'])
import LabelInput from "../../../components/LabelInput.vue"
// 第一个多边形
const onePolyId = ref()
const onePolyList = ref()
// 第二个多边形
const twoPolyId = ref()
const twoPolyList = ref()
const position = ref()
const intersectObjList: any = ref([])//存放计算好的在场景中但不在场景树上的对象
const destroy = () => {//销毁计算好的在场景中但不在场景树上的对象
    if (intersectObjList.value.length >= 1) {
        intersectObjList.value.forEach((item: any) => {
            if (item) {
                xbsjEarthUi.destroySceneObject(item)
            }
        })
    }
    intersectObjList.value = []
}
const compareArrays = (a: any, b: any) => {
    return JSON.stringify(a) === JSON.stringify(b);
};
const ok = () => {//点击计算
    destroy()
    if (!onePolyId.value) {
        Message.warning('请选择第一个多边形')
        return
    }
    if (!twoPolyId.value) {
        Message.warning('请选择第二个多边形')
        return
    }
    if (onePolyId.value === twoPolyId.value) {
        Message.warning('请选择不同的多边形')
        return
    }
    const sceneObject = xbsjEarthUi.getSceneObjectById(onePolyId.value) as ESGeoPolygon
    if (!sceneObject) return
    const result = sceneObject.geoPolygonOverlap(position.value)
    let polygonHierarchy: any;
    if (result === true) {
        polygonHierarchy = {
            points: sceneObject.points,
            innerRings: [position.value]
        }
    } else if (result === false) {
        polygonHierarchy = {
            points: position.value,
            innerRings: [sceneObject.points]

        }
    } else {
        const result2 = sceneObject.getDifference(position.value)
        if (!result2) {
            Message.warning('两个多边形无差异')
            return
        } else {
            const r = result2 as [number, number, number][][]
            r.forEach((item: any) => {
                const p = item
                if (compareArrays(p[0], p[p.length - 1])) p.pop()
                // console.log(p.length);
                if (sceneObject.points) {
                    if (p.length > sceneObject.points.length) {
                        polygonHierarchy = {
                            points: p,
                            innerRings: []
                        }
                    }
                }
            })
        }
    }
    if (polygonHierarchy) {
        const sceObj = xbsjEarthUi.createSceneObject(ESPolygonWithHole)
        if (!sceObj) return
        sceObj.points = polygonHierarchy.points
        sceObj.innerRings = polygonHierarchy.innerRings
        sceObj.name = `差异多边形`;
        intersectObjList.value.push(sceObj)
    } else {
        Message.warning('无法计算此种多边形差异')
        return
    }
}
const scene = () => {
    if (intersectObjList.value.length <= 0) {
        Message.warning('请先计算')
        return
    }
    const json = JSON.parse(intersectObjList.value[intersectObjList.value.length - 1].jsonStr)
    destroy()
    createSceneObjTreeItemFromJson(xbsjEarthUi, json)

}
//第一个多边形列表是否显示
const changeOnePolygonShow = () => {
    onePolyList.value = searchForPolygonValues(xbsjEarthUi)
    if (onePolyList.value.length <= 0) {
        Message.warning('多边形列表为空，请先创建多边形')
        return
    }
}
// 选择第一个多边形
const changeOnePolygonId = (item: any) => {
    onePolyId.value = item.id
}

////第二个多边形列表是否显示
const changeTwoPolygonShow = () => {
    twoPolyList.value = searchForPolygonValues(xbsjEarthUi)
    if (twoPolyList.value.length <= 0) {
        Message.warning('多边形列表为空，请先创建多边形')
        return
    }
}
// 选择第二个多边形
const changeTwoPolygonId = (item: any) => {
    twoPolyId.value = item.id
    position.value = item.position
}
onBeforeUnmount(() => {
    destroy()
})
</script>